package com.tocea.formation.Launcher;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tocea.formation.connection.HibernateUtil;
import com.tocea.formation.model.Event;
import com.tocea.formation.model.Person;


public class MainWithAssoc
{
    
    private static Logger LOGGER = LoggerFactory.getLogger(Main.class);
    
    /**
     * @param args
     */
    public static void main(String[] args) {
    
        Session session = HibernateUtil.getSession();
        
        Transaction transaction = session.beginTransaction();
        
        Person p = new Person();
        p.setForname("Jeremie");
        p.setName("Guidoux");
        
        Event event = new Event("TP Hibernate", p);
        p.addEvent(event);
        session.saveOrUpdate(event);
        session.saveOrUpdate(p);
        
        transaction.commit();
        
        List<Person> list = session.createCriteria(Person.class).list();
        for (Person person : list) {
            LOGGER.info(person.getName());
            for (Event  ev : person.getEvents()) {
                LOGGER.info(ev.toString());
            }
        }
        LOGGER.info("Affichage des evenements");
        List<Event> events = session.createCriteria(Event.class).list();
        LOGGER.info("nb Events = " + events.size());
        for (Event ev : events) {
            LOGGER.info(ev.getName());
        }
        session.flush();
        session.close();
        
        
        LOGGER.info("FINISH");
    }
    
}
